//
//  UIView+EasyFrame.m
//  HDCommon
//
//  Created by Huadong Zheng on 2023/4/11.
//

#import "UIView+EasyFrame.h"

@implementation UIView (EasyFrame)
-(CGFloat)width{
    return self.frame.size.width;
}

-(CGFloat)height{
    return self.frame.size.height;
}

-(CGFloat)y{
    return self.frame.origin.y;
}
-(CGFloat)x{
    return self.frame.origin.x;
}
-(void)setWidth:(CGFloat)width{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

-(void)setHeight:(CGFloat)height{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}
-(void)setX:(CGFloat)x{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}
-(void)setY:(CGFloat)y{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (void)setCenterX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}


- (void)setCenterY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}
- (CGFloat)centerX
{
    return self.center.x;
}



- (CGFloat)centerY
{
    return self.center.y;
}

-(CGFloat)maxX{
    return self.frame.origin.x + self.frame.size.width;
}
-(CGFloat)maxY{
    return self.frame.origin.y + self.frame.size.height;
}
-(void)setMaxX:(CGFloat)x{
    [self setX:x-[self width]];
}
-(void)setMaxY:(CGFloat)y{
    [self setY:y - [self height]];
}

-(void)setMaxWidth:(CGFloat)maxWidth{
    self.width = self.width > maxWidth ? maxWidth : self.width;
}

-(void)setMaxXForMaxWidth:(CGFloat)maxX{
    if (self.maxX > maxX) {
        self.width = maxX - self.x;
    }
}
-(void)setXForMaxWidth:(CGFloat)x{
    if (self.x < x) {
        self.width = self.maxX - x;
        self.x = x;
    }
}
-(void) setCenterYInSuperView{
    if (self.centerY != self.superview.height/2) {
        self.centerY = self.superview.height/2;
    }
    
}
-(void) setCenterXInSuperView{
    if (self.centerX != self.superview.width/2) {
        self.centerX = self.superview.width/2;
    }
}
-(void) setCenterYUpSuperViewCenter:(CGFloat) y{
    if (self.centerY != self.superview.height/2 - y) {
        self.centerY = self.superview.height/2 - y;
    }
}
-(void) setCenterYDownSuperViewCenter:(CGFloat) y{
    if (self.centerY != self.superview.height/2 + y) {
        self.centerY = self.superview.height/2 + y;
    }
    
}

-(void)setX:(CGFloat)x maxX:(CGFloat)maxX{
    self.x = x > maxX ? maxX : x;
}
-(void) setMaxX:(CGFloat) maxX minX:(CGFloat) minX{
    // .maxX = x < minX ? minX : ;
    self.maxX = maxX < minX ? minX : maxX;
}

-(void)setCenterXJiange:(CGFloat)jiange subView1:(UIView *)view subView2:(UIView *)view2{
    view.x = (self.width - jiange - view.width - view2.width)/2;
    view2.x = view.maxX + jiange;
}
-(void) setCenterYJiange:(CGFloat) jiange subView1:(UIView *) view subView2:(UIView *) view2{
    view.y = (self.height - jiange - view.height - view2.height)/2;
    view2.y = view.maxY + jiange;
}
-(void)setCenterYJiange:(CGFloat)jiange startY:(CGFloat)startY endY:(CGFloat)endY subView1:(UIView *)view subView2:(UIView *)view2{
    
    view.y = startY + (endY - startY - view.height - view2.height - jiange)/2;
    view2.y = view.maxY + jiange;
    
}
-(void) setRight:(CGFloat) right{
    self.maxX = self.superview.width - right;
}
-(void) setLeft:(CGFloat) left{
    self.x = left;
}
-(void) setTop:(CGFloat) top{
    self.y = top;
}
-(void) setBottom:(CGFloat) bottom{
    self.maxY = self.superview.height - bottom;
}
-(void)setWidthInCenter{
    self.width = self.superview.width - self.x * 2;
}

- (void)setMinX:(CGFloat)x{
    self.x = self.x < x ? x : self.x;
}
-(void)setMinY:(CGFloat)y{
    self.y = self.y < y ? y : self.y;
}
-(void)setCenterYWithJiange:(CGFloat)jiange subViews:(NSArray *)subViews{
    
    CGFloat subViewHeightAll = 0;
    for (UIView * view in subViews) {
        subViewHeightAll += view.height;
    }
    
    
    CGFloat y = (self.height - (subViews.count - 1) * jiange - subViewHeightAll)/2;
    for (UIView * view in subViews) {
        view.y = y;
        y = jiange + view.maxY;
    }
    
}
-(void) setUnhiddenViewCenterYWithJiange:(CGFloat) jiange subViews:(NSArray *) subViews{
    NSMutableArray * array = [NSMutableArray array];
    for (UIView * view in subViews) {
        view.hidden == false ? [array addObject:view] : 1 ;
    }
    [self setCenterYWithJiange:jiange subViews:array];
    
}
-(void) setCenterXWithJiange:(CGFloat) jiange subViews:(NSArray *) subViews{
    CGFloat subViewWidthAll = 0;
    for (UIView * view in subViews) {
        subViewWidthAll += view.width;
    }
    
    
    CGFloat x = (self.width - (subViews.count - 1) * jiange - subViewWidthAll)/2;
    for (UIView * view in subViews) {
        view.x = x;
        x = jiange + view.maxX;
    }
    
}
-(void) setUnhiddenViewCenterXWithJiange:(CGFloat) jiange subViews:(NSArray *) subViews{
    NSMutableArray * array = [NSMutableArray array];
    for (UIView * view in subViews) {
        view.hidden == false ? [array addObject:view] : 1 ;
    }
    [self setCenterXWithJiange:jiange subViews:array];
}

-(void) setCenterYWithJiange:(CGFloat) jiange subViews:(NSArray *) subViews fromY:(CGFloat) fromY toY:(CGFloat) toY{
    CGFloat subViewHeightAll = 0;
    for (UIView * view in subViews) {
        subViewHeightAll += view.height;
    }
    
    
    CGFloat y = (toY - fromY - (subViews.count - 1) * jiange - subViewHeightAll)/2 + fromY;
    for (UIView * view in subViews) {
        view.y = y;
        y = jiange + view.maxY;
    }
}
-(void) setUnhiddenViewCenterYWithJiange:(CGFloat) jiange subViews:(NSArray *) subViews fromY:(CGFloat) fromY toY:(CGFloat) toY{
    NSMutableArray * array = [NSMutableArray array];
    for (UIView * view in subViews) {
        view.hidden == false ? [array addObject:view] : 1 ;
    }
    [self setCenterYWithJiange:jiange subViews:array fromY:fromY toY:toY];
    
}
@end
